#include "VectorAttributeTable.h"
#include "VectorLayer.h"
#include <gdal/gdal_priv.h>
#include <gdal/ogr_geometry.h>
#include <gdal/ogrsf_frmts.h>
#include <QStringList>
#include <QTime>
#include <log/bdzhlog.h>
#include <QScrollBar>

VectorAttributeTable::VectorAttributeTable(VectorLayer* layer, QWidget *parent)
	: QWidget(parent)
	, _vectorLayer(layer)
	, _ogrLayer(nullptr)
{
	ui.setupUi(this);
	initialize();
}

VectorAttributeTable::~VectorAttributeTable()
{

}

void VectorAttributeTable::initialize()
{
	::OGRRegisterAll();
	const QString filePath = _vectorLayer->getFilePath();
	auto dataSource = OGRSFDriverRegistrar::Open(filePath.toStdString().c_str(), FALSE );
	_ogrLayer = dataSource->GetLayer(0);
	if(nullptr == _ogrLayer) 
	{
		OGRDataSource::DestroyDataSource(dataSource);
		return;
	}
	QStringList header;
	OGRFeatureDefn *featureDefn = _ogrLayer->GetLayerDefn();
	for( int i = 0; i < featureDefn->GetFieldCount(); i++ )  
	{ 
		OGRFieldDefn *fieldDfn = featureDefn->GetFieldDefn( i );  
		const char * typeName = fieldDfn->GetNameRef();
		header << typeName;
	}
	QStringList headerAlis;
	for (int i = 0 ; i < header.length(); i++)
	{
		int tmpCount = 0;
		QString tmpStr = header[i];
		for (int j = 0; j < i; j++)
		{
			if (header[j].compare(tmpStr) == 0)
			{
				tmpCount++;
			}
		}
		if (tmpCount > 0)
		{
			headerAlis.push_back(tmpStr + "_" + QString::number(tmpCount));
		}
		else
			headerAlis.push_back(tmpStr);
		
	}
	ui.tableWidget->setColumnCount(featureDefn->GetFieldCount());
	ui.tableWidget->setRowCount(_ogrLayer->GetFeatureCount());
	ui.tableWidget->setHorizontalHeaderLabels(headerAlis);

	_featureIds = _vectorLayer->getFeatureIds();
	

	int rowHeight = ui.tableWidget->rowHeight(1);                                               
	int scrollHeight = ui.tableWidget->maximumViewportSize ().height(); 
	float rowNumber = (float)scrollHeight/(float)rowHeight + 1;
	_tablePageRowCount = (int)rowNumber;
	if (rowNumber - _tablePageRowCount > 0.000001)
	{
		_tablePageRowCount++;
	}
	QScrollBar * bar = ui.tableWidget->verticalScrollBar();
	connect(bar , SIGNAL(valueChanged( int )) ,this , SLOT(resetTable(int)));
	resetTable(0);
}

void VectorAttributeTable::resetTable( int scrollBarValue )
{
	int realRowN = scrollBarValue; 

	int rowNchanged = 0;
	QTableWidgetItem * item ;
	if((int)_featureIds.size() - realRowN < _tablePageRowCount )
	{
		rowNchanged = _featureIds.size() - realRowN;
	}
	for (int i = 0 ; i < _tablePageRowCount ; i++)
	{
		if( i == rowNchanged && i + realRowN == ui.tableWidget->rowCount() ) 
			break;
		
		if((int)_featureIds.size() <= i + realRowN) return;
		OGRFeature *feature = _ogrLayer->GetFeature(_featureIds[i + realRowN]);
		for (int j = 0; j < feature->GetFieldCount(); ++j)
		{
			QString field = feature->GetFieldAsString(j);
			item = new QTableWidgetItem(field);
			ui.tableWidget->setItem(i + realRowN , j , item);
		}
		OGRFeature::DestroyFeature(feature);
	}
}
